<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属商户" prop="merchantId">
        <el-select v-model="queryParams.merchantId" placeholder="请选择所属商家" clearable
                   size="small" >
          <el-option  v-for="merchant in merchantList"
                      :key="merchant.deptId"
                      :label="merchant.deptName"
                      :value="merchant.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="视频分组" prop="groupId">
        <el-select v-model="queryParams.groupId" placeholder="请选择视频分组" clearable
                   size="small" >
          <el-option  v-for="resourceGroup in resourceGroupList"
                      :key="resourceGroup.id"
                      :label="resourceGroup.groupName"
                      :value="resourceGroup.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="视频类型" prop="resourceType">
        <el-select v-model="queryParams.resourceType" placeholder="请选择视频类型" clearable
                   size="small"  >
          <el-option
            v-for="dict in resourceTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['merchant:resource:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['merchant:resource:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:resource:remove']"
        >删除</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="resourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属商户" align="center" prop="merchantName" />
      <el-table-column label="视频分组" align="center" prop="groupName" />
      <el-table-column label="视频类型" align="center" prop="resourceType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.resourceType == 0" type="success">图片</el-tag>
          <el-tag v-if="scope.row.resourceType == 1" type="warning">视频</el-tag>
          <el-tag v-if="scope.row.resourceType == 2" type="danger">音频</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="视频地址" align="center" prop="resourceUrl" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:resource:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:resource:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

   <!-- <div >
    <el-row v-loading="loading" :gutter="20" >
      <el-col :span="4" v-for="(resource,i) in resourceList" style="margin-top:15px;">
        <span>{{resource.merchantName}}-{{resource.groupName}}</span>
        <div class="grid-content bg-purple">
          <template v-if="resource.resourceType==0">
            <img :src="resource.resourceUrl"/>
          </template>
        <template v-if="resource.resourceType==1">
          <video :src="resource.resourceUrl" controls="controls" style="width:100%;height:100%;object-fit:fill">
          您的浏览器不支持 video标签video
          </video>
        </template>
        <template v-if="resource.resourceType==2">
          <video :src="resource.resourceUrl" controls="controls">
            您的浏览器不支持 video标签
          </video>
          </template>
      </div>
        <div style="text-align: center">
          <el-tag type="success">id={{resource.id}}</el-tag>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(resource)"
            v-hasPermi="['merchant:resource:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(resource)"
            v-hasPermi="['merchant:resource:remove']"
          >删除</el-button>
        </div>
      </el-col>
    </el-row>
  </div>-->

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px"  v-loading="loading"  element-loading-text="加速上传中">
        <el-form-item label="所属商户" prop="merchantId">
          <el-select v-model="form.merchantId" placeholder="请选择所属商家" clearable
                     size="small" >
            <el-option  v-for="merchant in merchantList"
                        :key="merchant.deptId"
                        :label="merchant.deptName"
                        :value="merchant.deptId" />
          </el-select>
        </el-form-item>
        <el-form-item label="视频分组" prop="groupId">
          <el-select v-model="form.groupId" placeholder="请选择视频分组" clearable
                     size="small" >
            <el-option  v-for="resourceGroup in resourceGroupList"
                        :key="resourceGroup.id"
                        :label="resourceGroup.groupName"
                        :value="resourceGroup.id" />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="视频类型" prop="resourceType">
          <el-select v-model="form.resourceType" placeholder="请选择视频类型" @change="selectResourceType">
            <el-option
              v-for="dict in resourceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="上传视频" prop="resourceUrl">
          <el-input v-model="form.resourceUrl" v-show="false" placeholder="请输入视频地址" />

          <el-upload  ref="upload" :limit="10" :http-request="requestUpload" :file-list="fileList" action="#"  multiple
                      :before-upload="beforeUpload"  :accept="accept" >
            <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listMerchant } from "@/api/merchant/merchant";
import {   listResourcegroup } from "@/api/merchant/resourcegroup";
import { listResource, getResource, delResource, addResource, updateResource, exportResource } from "@/api/merchant/resource";
  import { upload } from "@/api/upload";
  import { listDept } from "@/api/system/dept";
export default {
  name: "Resource",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户视频表格数据
      resourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 12,
        merchantId: null,
        groupId: null,
        resourceType: null,
        resourceUrl: null,
      },
      // 表单参数
      form: {
          resourceType:0,
          resourceUrl:''
      },
      // 表单校验
      rules: {
        merchantId: [
          { required: true, message: "商户不能为空", trigger: "blur" }
        ],
        groupId: [
          { required: true, message: "分组不能为空", trigger: "blur" }
        ],
        resourceType: [
          { required: true, message: "视频类型不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "视频名称不能为空", trigger: "blur" }
        ],
        resourceUrl: [
          { required: true, message: "上传视频不能为空", trigger: "blur" }
        ],
      },
      //商户列表
      merchantList: [],
      //视频分组列表
      resourceGroupList: [],
      // 视频类型状态字典
      resourceTypeOptions: [],
      fileList: [],
      accept:''
    };
  },
  created() {
    this.getList();
    this.getMerchantList();
    /*this.getDicts("resource_type").then(response => {
      this.resourceTypeOptions = response.data;
    });*/
    this.selectResourceType(1);
    this.getResourcegroupList();
  },
  methods: {
    /** 查询商户列表 */
    getMerchantList() {
      this.loading = true;
      listDept({deptType:2}).then(response => {
        this.merchantList = response.data;
        this.loading = false;
      });
    },
      queryResourcegroup(){
        //根据商户查询视频分组
      this.loading = true;
      listResourcegroup().then(response => {
        this.resourceGroupList = response.rows;
        this.loading = false;
      });
    },
    getResourcegroupList(){
      this.loading = true;
      listResourcegroup().then(response => {
        this.resourceGroupList = response.rows;
        this.loading = false;
      });
    },
    selectResourceType(value){//选择上传的视频类型后，要对上传控件进行配置，避免传错文件
      if(value==0){
        this.accept = "image/*";
      }
      if(value==1){
        this.accept = "video/*,audio/*";
      }
      if(value==2){
        this.accept = "audio/*";
      }
    },
    // 覆盖默认的上传行为
    requestUpload(params) {
      var that = this;
      that.loading = true;
      const _file = params.file;
      let formData = new FormData();
      formData.append("file", _file);
      upload(formData).then(response => {
//          this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl;
        this.form.resourceUrl = this.form.resourceUrl+","+response.url;
        this.msgSuccess("上传成功");
        that.loading = false;
      });
    },
    beforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 10
      if (!isRightSize) {
        this.$message.error('文件大小超过 10MB')
      }
      let isAccept = true;
      /*let isAccept = new RegExp(this.accept).test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择video/!*类型的文件')
      }*/
      return isRightSize && isAccept;
    },
    /** 查询商户视频列表 */
    getList() {
      this.loading = true;
      listResource(this.queryParams).then(response => {
        this.resourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        merchantId: null,
        groupId: null,
        resourceType: null,
        resourceUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户视频";

      //清空
       this.$refs.upload.clearFiles();


    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResource(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户视频";
      });
      //清空
      this.$refs.upload.clearFiles();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResource(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResource(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商户视频编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delResource(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商户视频数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportResource(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style>
  .el-row {
    margin-bottom: 20px;
  &:last-child {
     margin-bottom: 0;
   }
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
    height:160px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>
